package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduTeacherMapper;
import com.zenith.edu.po.domain.EduTeacher;
import com.zenith.edu.po.dto.EduTeacherDTO;
import com.zenith.edu.po.vo.EduTeacherVO;
import com.zenith.edu.service.EduTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    
    @Override
    public EduTeacherVO createTeacher(EduTeacherDTO dto) {
        EduTeacher teacher = new EduTeacher();
        BeanUtils.copyProperties(dto, teacher);
        this.save(teacher);
        
        EduTeacherVO vo = new EduTeacherVO();
        BeanUtils.copyProperties(teacher, vo);
        return vo;
    }
    
    @Override
    public EduTeacherVO getTeacherById(Long id) {
        EduTeacher teacher = this.getById(id);
        if (teacher == null) {
            return null;
        }
        
        EduTeacherVO vo = new EduTeacherVO();
        BeanUtils.copyProperties(teacher, vo);
        return vo;
    }
    
    @Override
    public EduTeacherVO updateTeacher(EduTeacherDTO dto) {
        EduTeacher teacher = new EduTeacher();
        BeanUtils.copyProperties(dto, teacher);
        this.updateById(teacher);
        
        EduTeacherVO vo = new EduTeacherVO();
        BeanUtils.copyProperties(teacher, vo);
        return vo;
    }
    
    @Override
    public boolean deleteTeacher(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduTeacherVO> getTeacherList() {
        List<EduTeacher> teacherList = this.list();
        return teacherList.stream().map(teacher -> {
            EduTeacherVO vo = new EduTeacherVO();
            BeanUtils.copyProperties(teacher, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}